package org.example.redis.demo;

import jakarta.annotation.Resource;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ClusterOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

//负责对string类型字符串进行操作
@Component
@Slf4j
public class StringRedisDemo {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    //存放
    public void a(){
        //string类型字符串进行存放操作
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("A", "aa");//set A aa
        ops.set("B", "bb");
        //存放时设置存放时间
        ops.set("C", "cc",1, TimeUnit.HOURS);
        ops.setIfPresent("A","aa1");//存在Key 覆盖
        ops.setIfPresent("D","dd1");//不存在Key 不存放
        ops.setIfPresent("B","bb1");//存在Key 不覆盖
        ops.setIfPresent("E","ee1");//不存在Key 存放
    }
    //获取
    public void b(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String a = ops.get("A");
        String b = ops.get("B");
        String c = ops.get("C");
        String d = ops.get("D");
        String e = ops.get("E");
        log.info("a:{},b:{},c:{},d:{},e:{}",a,b,c,d,e);
    }
    //其他
    public void c(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        //删除
        stringRedisTemplate.delete("A");
        //追加
        ops.append("B","abcd12344");
        //自增
        ops.increment("D");//d+1
        ops.increment("D",3);//d+3
        ops.decrement("D");//d-1
        ops.decrement("D",2);//d-2
    }
}
